package com.dayaokeji.rhythmschoolstudent.client.mine;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.dayaokeji.rhythmschoolstudent.R;
import com.dayaokeji.rhythmschoolstudent.client.common.MultiListFragment;
import com.dayaokeji.rhythmschoolstudent.client.home.meeting.MeetingDetailActivity;
import com.dayaokeji.rhythmschoolstudent.client.home.meeting.adapter.MeetingListAdapter;
import com.dayaokeji.rhythmschoolstudent.utils.AppExitUtil;
import com.dayaokeji.rhythmschoolstudent.utils.UserUtils;
import com.dayaokeji.server_api.ApiUtils;
import com.dayaokeji.server_api.api.MeetingApi;
import com.dayaokeji.server_api.domain.Meeting;
import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration;

import java.io.Serializable;

import retrofit2.Call;

/**
 * Created by Gorden on 2017/10/16.
 * 会议列表
 */

public class MeetingSearchListFragment extends MultiListFragment<Meeting,BaseViewHolder> {

    private boolean isQuery = false;
    private String id;
    private String keyword = "";

    public static MeetingSearchListFragment newInstance() {

        Bundle args = new Bundle();

        MeetingSearchListFragment fragment = new MeetingSearchListFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        if (UserUtils.getUserInfo() == null) {
            AppExitUtil.exit(getActivity());
        }
        id = UserUtils.getUserInfo().getId();

        setAdapter(new MeetingListAdapter());

        setOnListener();
    }

    private void setOnListener() {
        getAdapter().setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (adapter.getData().size() >= position) {
                    Intent intent = new Intent(getActivity(), MeetingDetailActivity.class);
                    intent.putExtra(MeetingDetailActivity.EXTRA_MEETING_ENTITY, (Serializable) adapter.getData().get(position));
                    startActivity(intent);
                }

            }
        });
    }

    public void queryKeyword(String keyword) {
        this.isQuery = true;
        this.keyword = keyword;
        onRefresh();
    }

    @Override
    protected Call createCall(int offset, int max) {
        if (isQuery) {
            return ApiUtils.getApi(MeetingApi.class).createMeetingHistory(id,offset,max,keyword);
        }else{
            return ApiUtils.getApi(MeetingApi.class).createMeetingHistory(id, "2017-01-01",offset,max);
        }
    }

    @Override
    protected Call createCall2(int offset, int max) {
        if (isQuery) {
            return ApiUtils.getApi(MeetingApi.class).userMeetingHistory(id,offset,max,keyword);
        }else{
            return ApiUtils.getApi(MeetingApi.class).userMeetingHistory(id,"2017-01-01",offset,max);
        }
    }
}
